﻿function animate_text() {
    var current = parseInt($("#hiddenCurrentIndex").val());
    var $target = $("div[data-index=" + current + "]");

    switch (current) {
        case 0:
            $target.find("[text-index=0]").fadeIn('slow', function () {
                $target.find("[text-index=1]").fadeIn('slow', function () {
                    $target.find("[text-index=2]").fadeIn('normal', function () {
                        $target.find("[text-index=2]").animate({ top: "0px" }, 'slow', function () {
                            $target.find("[text-index=3]").fadeIn('slow', function () {

                            });
                        });
                    });
                });
            });
            break;

        case 1:
            //$target.find(".chuangqi").fadeIn('slow', function () {
            $target.find(".chuangqi").animate({ left: "0px", opacity: 1 }, 3000, function () { });
            //});
            break;

        case 2:
            $target.find("[data-type=common]").fadeIn(3000);
            $target.find("[data-type=left]").animate({ left: "0px", opacity: 1 }, 3000, function () { });
            break;

        case 3:
            var duration3 = 2000;
            $target.find("[img-index=0]").animate({ left: "0px", opacity: 1 }, duration3, function () {
                $target.find("[img-index=1]").animate({ left: "0px", opacity: 1 }, duration3, function () {
                    $target.find("[img-index=2]").animate({ left: "0px", opacity: 1 }, duration3, function () {
                        $target.find("[img-index=3]").animate({ left: "0px", opacity: 1 }, duration3, function () {

                        });
                    });
                });
            });
            break;

        case 4:
            var ducation4 = 1500;
            $target.find(".arrow-index-0").animate({ left: "0px", opacity: 1 }, ducation4, function () {
                $target.find(".arrow-index-1").animate({ left: "0px", opacity: 1 }, ducation4, function () {
                    $target.find(".arrow-index-2").animate({ left: "0px", opacity: 1 }, ducation4, function () {

                    });
                });
            });
            break;

        default:
            break;
    }
}

var current_hor_index = 0;

$(document).ready(function () {
    var height = document.body.clientHeight;
    var width = document.body.clientWidth;
    var $current = $("#hiddenCurrentIndex");
    var $max = $("#hiddenMax");
    var $arrowdown = $(".arrow-down");
    var $arrowleft = $(".arrow-left");
    var $arrowright = $(".arrow-right");

    $("div[data-index]").each(function () {
        var $obj = $(this);
        $obj.css('top', (parseInt($obj.attr("data-index")) * height) + "px");
    });

    $("div[data-hor-index]").each(function () {
        var $obj = $(this);
        $obj.css('left', (parseInt($obj.attr("data-hor-index")) * width) + "px");
    });

    $(".arrow-down,#imgBaoming").click(function () {
        var current = parseInt($current.val());

        for (var i = 0; i <= (parseInt($max.val()) - 1) ; i++) {
            var $target = $("div[data-index=" + i + "]");
            var newtop = (parseInt($target.css('top').replace('px', '')) - height) + "px";
            $target.animate({ top: newtop });
        }

        current = current + 1;
        $current.val(current);

        animate_text();

        if (3 == current) {
            $arrowdown.hide();
        }

        if (1 == current) {
            if (current_hor_index != 0) {
                $arrowleft.show();
            }
            else {
                $arrowleft.hide();
            }
            if (current_hor_index != (parseInt($("div[data-hor-index]").length) - 1)) {
                $arrowright.show();
            }
            else {
                $arrowright.hide();
            }
        }
        else {
            $arrowleft.hide();
            $arrowright.hide();
        }
    });

    $arrowleft.click(function () {
        for (var i = 0; i <= (parseInt($("div[data-hor-index]").length) - 1) ; i++) {
            var $target = $("div[data-hor-index=" + i + "]");
            var newleft = (parseInt($target.css('left').replace('px', '')) + width) + "px";
            $target.animate({ left: newleft });
        }
        
        current_hor_index -= 1;

        if (current_hor_index != 0) {
            $arrowleft.show();
        }
        else {
            $arrowleft.hide();
        }
        if (current_hor_index != (parseInt($("div[data-hor-index]").length) - 1)) {
            $arrowright.show();
        }
        else {
            $arrowright.hide();
        }
    });

    $arrowright.click(function () {
        for (var i = 0; i <= (parseInt($("div[data-hor-index]").length) - 1) ; i++) {
            var $target = $("div[data-hor-index=" + i + "]");
            var newleft = (parseInt($target.css('left').replace('px', '')) - width) + "px";
            $target.animate({ left: newleft });
        }

        current_hor_index += 1;

        if (current_hor_index != 0) {
            $arrowleft.show();
        }
        else {
            $arrowleft.hide();
        }
        if (current_hor_index != (parseInt($("div[data-hor-index]").length) - 1)) {
            $arrowright.show();
        }
        else {
            $arrowright.hide();
        }
    });

    animate_text();

    //set video
    $(".video-pause").click(function () {
        document.getElementById("audioPlayer").play();
        $(this).hide().next().show();
    });

    $(".video-play").click(function () {
        document.getElementById("audioPlayer").pause();
        $(this).hide().prev().show();
    });
});